/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "gamelib.h"


uint		m_randSeed = 0;


/*
 ==================
 M_NormalToBytes
 ==================
*/
void M_NormalToBytes (const odVec3 &normal, byte bytes[2]){

	int		lat, lng;

	// Check for singularities
	if (normal.x == 0.0f && normal.y == 0.0f){
		if (normal.z > 0.0f){
			bytes[0] = 0;
			bytes[1] = 0;
		}
		else {
			bytes[0] = 128;
			bytes[1] = 0;
		}

		return;
	}

	lat = M_FtolFast(RAD2DEG(M_ATan2(normal.y, normal.x)) * (255.0f/360.0f));
	lat &= 255;

	lng = M_FtolFast(RAD2DEG(M_ACos(normal.z)) * (255.0f/360.0f));
	lng &= 255;

	bytes[0] = lng;
	bytes[1] = lat;
}

/*
 ==================
 M_BytesToNormal
 ==================
*/
void M_BytesToNormal (const byte bytes[2], odVec3 &normal){

	float	lat, lng;
	float	sLat, cLat, sLng, cLng;

	lng = (float)bytes[0] * (M_PI / 128.0f);
	lat = (float)bytes[1] * (M_PI / 128.0f);

	M_SinCos(lat, &sLat, &cLat);
	M_SinCos(lng, &sLng, &cLng);

	normal.Set(sLng * cLat, sLng * sLat, cLng);
}
